<template>
	<view class="history-container">
		<!-- 阅读统计 -->
		<view class="reading-stats">
			<view class="stats-item">
				<text class="num">{{ totalReadingTime }}</text>
				<text class="label">总阅读时长</text>
			</view>
			<view class="stats-item">
				<text class="num">{{ todayReadingTime }}</text>
				<text class="label">今日阅读</text>
			</view>
			<view class="stats-item">
				<text class="num">{{ bookCount }}</text>
				<text class="label">读过的书</text>
			</view>
		</view>
		
		<!-- 阅读时长走势 -->
		<view class="reading-trend">
			<view class="section-title">
				<text>阅读时长走势</text>
				<view class="tab-group">
					<text 
						v-for="(tab, index) in trendTabs" 
						:key="index"
						:class="['tab-item', { active: currentTrendTab === index }]"
						@tap="switchTrendTab(index)"
					>{{ tab }}</text>
				</view>
			</view>
			<view class="trend-chart">
				<!-- 这里可以接入图表组件，如 uCharts 或 echarts -->
				<image class="/static/生成读书时长图.png" mode="widthFix"></image>
			</view>
		</view>
		
		<!-- 阅读历史列表 -->
		<view class="reading-list">
			<view class="section-title">
				<text>阅读历史</text>
				<view class="filter-btn" @tap="showFilterPopup">
					<text>{{ currentFilter }}</text>
					<text class="arrow-down">▼</text>
				</view>
			</view>
			
			<view class="history-item" v-for="(item, index) in historyList" :key="index" @tap="goToReader(item)">
				<view class="date-info">
					<text class="date">{{ item.date }}</text>
					<text class="time">{{ item.readingTime }}分钟</text>
				</view>
				<view class="book-info">
					<image class="book-cover" :src="item.bookCover" mode="aspectFill"></image>
					<view class="book-detail">
						<text class="book-name">{{ item.bookName }}</text>
						<text class="chapter-name">{{ item.chapterName }}</text>
						<view class="progress-bar">
							<view class="progress" :style="{ width: item.progress + '%' }"></view>
						</view>
						<text class="progress-text">阅读至 {{ item.progress }}%</text>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 筛选弹窗 -->
		<uni-popup ref="filterPopup" type="bottom">
			<view class="filter-popup">
				<view class="popup-header">
					<text>筛选</text>
					<text class="close-btn" @tap="hideFilterPopup">×</text>
				</view>
				<view class="filter-list">
					<view 
						class="filter-item" 
						v-for="(filter, index) in filterOptions" 
						:key="index"
						:class="{ active: currentFilter === filter }"
						@tap="selectFilter(filter)"
					>{{ filter }}</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
export default {
	data() {
		return {
			totalReadingTime: '168小时',
			todayReadingTime: '2.5小时',
			bookCount: 26,
			trendTabs: ['周', '月', '年'],
			currentTrendTab: 0,
			currentFilter: '全部',
			filterOptions: ['全部', '本周', '本月', '今年'],
			historyList: [
				{
					date: '2024-03-20',
					readingTime: 30,
					bookName: '三体',
					bookCover: '/static/875f-kpamyih7048893.jpg',
					chapterName: '第三章 射手和农场主',
					progress: 35
				},
				{
					date: '2024-03-19',
					readingTime: 45,
					bookName: '活着',
					bookCover: '/static/01d7a06376f6a0000ed21000391d95 - 副本.jpg',
					chapterName: '第八章 变故',
					progress: 68
				}
				// 更多历史记录...
			]
		}
	},
	methods: {
		switchTrendTab(index) {
			this.currentTrendTab = index
			// 根据选中的时间维度获取对应的阅读时长数据
			this.getReadingTrendData(index)
		},
		getReadingTrendData(timeType) {
			// 获取阅读时长走势数据
			console.log('获取阅读时长走势数据:', timeType)
		},
		showFilterPopup() {
			this.$refs.filterPopup.open()
		},
		hideFilterPopup() {
			this.$refs.filterPopup.close()
		},
		selectFilter(filter) {
			this.currentFilter = filter
			this.hideFilterPopup()
			// 根据筛选条件获取历史记录
			this.getHistoryList(filter)
		},
		getHistoryList(filter) {
			// 获取阅读历史记录
			console.log('获取阅读历史记录:', filter)
		},
		goToReader(item) {
			uni.navigateTo({
				url: `/pages/reader/reader?id=${item.bookId}&chapter=${item.chapterId}`
			})
		}
	}
}
</script>

<style lang="scss">
.history-container {
	min-height: 100vh;
	background: #f5f5f5;
	padding: 20rpx;
	
	.reading-stats {
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		display: flex;
		justify-content: space-around;
		
		.stats-item {
			text-align: center;
			
			.num {
				font-size: 36rpx;
				font-weight: bold;
				color: #333;
				display: block;
				margin-bottom: 10rpx;
			}
			
			.label {
				font-size: 26rpx;
				color: #999;
			}
		}
	}
	
	.reading-trend {
		margin-top: 20rpx;
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		
		.section-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.tab-group {
				display: flex;
				
				.tab-item {
					padding: 10rpx 20rpx;
					font-size: 26rpx;
					color: #666;
					
					&.active {
						color: #3cc51f;
					}
				}
			}
		}
		
		.trend-chart {
			height: 300rpx;
			
			.chart-placeholder {
				width: 100%;
			}
		}
	}
	
	.reading-list {
		margin-top: 20rpx;
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		
		.section-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.filter-btn {
				font-size: 26rpx;
				color: #666;
				
				.arrow-down {
					font-size: 20rpx;
					margin-left: 10rpx;
				}
			}
		}
		
		.history-item {
			margin-bottom: 30rpx;
			
			.date-info {
				display: flex;
				justify-content: space-between;
				margin-bottom: 20rpx;
				
				.date {
					font-size: 26rpx;
					color: #999;
				}
				
				.time {
					font-size: 26rpx;
					color: #3cc51f;
				}
			}
			
			.book-info {
				display: flex;
				
				.book-cover {
					width: 120rpx;
					height: 160rpx;
					border-radius: 10rpx;
				}
				
				.book-detail {
					flex: 1;
					margin-left: 20rpx;
					
					.book-name {
						font-size: 28rpx;
						font-weight: bold;
						color: #333;
						margin-bottom: 10rpx;
						display: block;
					}
					
					.chapter-name {
						font-size: 26rpx;
						color: #666;
						margin-bottom: 20rpx;
						display: block;
					}
					
					.progress-bar {
						height: 6rpx;
						background: #f0f0f0;
						border-radius: 3rpx;
						margin-bottom: 10rpx;
						
						.progress {
							height: 100%;
							background: #3cc51f;
							border-radius: 3rpx;
						}
					}
					
					.progress-text {
						font-size: 24rpx;
						color: #999;
					}
				}
			}
		}
	}
	
	.filter-popup {
		background: #fff;
		border-radius: 20rpx 20rpx 0 0;
		
		.popup-header {
			padding: 30rpx;
			text-align: center;
			position: relative;
			border-bottom: 1rpx solid #f5f5f5;
			
			.close-btn {
				position: absolute;
				right: 30rpx;
				top: 30rpx;
				font-size: 40rpx;
				color: #999;
			}
		}
		
		.filter-list {
			padding: 30rpx;
			
			.filter-item {
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				font-size: 28rpx;
				color: #333;
				
				&.active {
					color: #3cc51f;
				}
			}
		}
	}
}
</style> 